﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using System.Diagnostics;
using RN.ERP.Web.PersonService;
using RN.ERP.Web.ModelService;
using Fly.Common.Linq;

namespace RN.ERP.Web.Handlers
{
    /// <summary>
    /// MenuHandler 的摘要说明
    /// </summary>
    public class MenuHandler : BaseHandler
    {
        public override void ProcessRequest(HttpContext context)
        {
          
            string menu = this.InitMenu();
            UtilitiesTool.OutputData(menu, string.Empty);
        }

        public override bool IsReusable
        {
            get
            {
                return false;
            }
        }

        #region 主导航

        /// <summary>
        /// 加一级菜单
        /// </summary>
        protected string InitMenu()
        {
            if (UserInfo == null)
            {
                return string.Empty;
            }

            List<MenuEntity> menus = new List<MenuEntity>();
            List<AuthorityService.T_Sys_Function> sysFunctions = null;
            using (AuthorityService.AuthorityServiceClient client = new AuthorityService.AuthorityServiceClient())
            {
                sysFunctions = JsonConvert.DeserializeObject<List<AuthorityService.T_Sys_Function>>(client.GetUserMenus(UserInfo.CorpUserGuid, "", HttpContext.Current.Application[ApplicationConstant.APPLICATION_ID].ToString()));
            }
            var sysFunction = sysFunctions.Where(f => f.ParenGuid == Guid.Empty).ToList();
            foreach (AuthorityService.T_Sys_Function sysFunc in sysFunction)
            {
                //菜单。
                MenuEntity menu = new MenuEntity();
                menu.text = sysFunc.Title;
                menu.isexpand = true;

                //子菜单
                if (sysFunctions.Where(f => f.ParenGuid == sysFunc.FunctionGuid).Count() > 0)
                {
                    menu.children = InitSubMenu(sysFunc.FunctionGuid, sysFunc.Title, sysFunctions);
                }
                menus.Add(menu);
            }

            MenuSpecialDeal(ref menus);
            return JsonConvert.SerializeObject(menus);
        }

        /// <summary>
        /// 加子菜单模板容器
        /// </summary>
        private List<MenuChildren> InitSubMenu(Guid FunctionGuid, string topMenuName, List<AuthorityService.T_Sys_Function> sysFunctions)
        {
            List<MenuChildren> menuChildren = new List<MenuChildren>();
            var sysFunction = sysFunctions.Where(f => f.ParenGuid == FunctionGuid).ToList();
            foreach (var child in sysFunction)
            {
                MenuChildren menuChild = new MenuChildren();
                menuChild.url = string.Format(AddTokenParam(child.Url), UserInfo.CorpUserGuid);
                menuChild.text = child.Title;
                menuChildren.Add(menuChild);
            }
            return menuChildren;
        }

        private string AddTokenParam(string url)
        {
            if (!string.IsNullOrEmpty(url))
            {
             
                string token = HttpContext.Current.Session["token2"] == null ? string.Empty : HttpContext.Current.Session["token2"].ToString();
                if (!String.IsNullOrEmpty(token))
                {
                    if (url.Contains('?'))
                    {
                        url += "&Token=" + token;
                    }
                    else
                    {
                        url += "?Token=" + token;
                    }
                }
            }
            return url;
        }

        /// <summary>
        /// 特殊情况时对菜单的特别处理
        /// </summary>
        private void MenuSpecialDeal(ref List<MenuEntity> menus)
        {
            if (AppID == "95")
            {
                Guid userGuid = Guid.Empty;
                if (!Guid.TryParse(UserInfo.CorpUserGuid, out userGuid))
                {
                    return;
                }
                if (UserInfo.RoleType == "Expert")
                {
                    OldExpertMenuDeal(ref menus, userGuid);
                }
                else if (UserInfo.RoleType == "Manager")
                {
                    ProvinceManagerMenuDeal(ref menus, userGuid);
                }
            }
        }

        #endregion

        #region 针对老专家的处理
        /// <summary>
        /// 专家端菜单处理
        /// </summary>
        private void OldExpertMenuDeal(ref List<MenuEntity> menus,Guid userGuid)
        {
            //if (String.IsNullOrEmpty(UserInfo.UserTel))
            //{
            //    return;
            //}
            //using (PersonServiceClient personServiceClient = new PersonServiceClient())
            //{
            //    QueryPersonInfoCondition condition = new QueryPersonInfoCondition();
            //    condition.EmpGuid = new ParameterValue<Guid>() { V = userGuid, O = OptEnum.Equal };
            //    List<QueryPersonInfoCondition> list = new List<QueryPersonInfoCondition>();
            //    list.Add(condition);
            //    tb_Emp_Manager exp = JsonConvert.DeserializeObject<tb_Emp_Manager>(personServiceClient.GetExpertInfo(JsonConvert.SerializeObject(list), string.Empty));
            //    if (exp != null && exp.isShenBao == 3)////isShenBao是3表明是老专家
            //    {
            //        var menu = menus.FirstOrDefault(m => m.text == "初始登记" || m.text == "专家信息");
            //        if (menu != null)
            //        {
            //            menu.text = "资格管理";
            //            var child = menu.children.FirstOrDefault(c => c.text == "初始申报" || c.text == "专家基本信息");
            //            if (child != null)
            //            {
            //                child.text = "专家信息完善";
            //            }
            //        }
            //    }
            //}
            var menu = menus.FirstOrDefault(m => m.text == "初始登记" || m.text == "专家信息");
            if (menu != null)
            {
                menu.text = "资格管理";
                var child = menu.children.FirstOrDefault(c => c.text == "初始申报" || c.text == "专家基本信息");
                if (child != null)
                {
                    child.text = "专家信息完善";
                }
            }
        }

        private void ProvinceManagerMenuDeal(ref List<MenuEntity> menus, Guid userGuid)
        {
            var menu = menus.FirstOrDefault(m => m.text == "初始审核" || m.text == "专家管理");
            if (menu != null)
            {
                menu.text = "资格管理";
                MenuChildren mc = new MenuChildren
                {
                    text = "专家信息完善",
                    url = "Person/PersonEmpStatisticOldExpert.aspx"
                };
                menu.children.Add(mc);
            }
        }
        #endregion
    }
}